var app = getApp()
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var sliderPanel = require('../../components/sliderPanel/sliderPanel');
var banner = require('../widget/banner/banner');
var itemBanner = require('../widget/itemBanner/itemBanner');
var commodity = require('../widget/commodityDisplay/commodityDisplay');
var storage = require('../../utils/storage');


var shareContent = {
    'title':'企鹅优品－优质生活指南',
    'desc':'腾讯精品体验平台，为用户提供优质的生活方式和购物体验。',
    'path':'/pages/project-index/index'
}
var shareObj = utils.share(shareContent);
var pageSetting = {
    callback:function(componentName){
        switch(componentName){
            case 'sliderBar':
                var type = arguments[1];
                var id = arguments[2];
                this.setData({
                    wiiSliderBar:{
                        isActive:id,
                        data:this.data['wiiSliderBar']['data']
                    }
                });
            break;
            case 'sliderPanel':
                var current = arguments[1];
                wx.previewImage({
                    current: current,
                    urls: mock.imageList
                });
            break;
            default:
            break;
        }
    },
    data: {
        lists:[{},{},{}],
        comSpecials:[],
        cartCount:0 
    },
    onLoad: function(options) {
        app.globalData.cartCountState = false;
        modal.loading();
        var that = this,
            tempBanner = that.data['comBanner'],
            tempCommodity = that.data['comCommodity'],
            tempSpecials = that.data['comSpecials'];  
        var datas={
            indexData:{
                'self:myshop:homepage:index_data':{
                    agencyId:10000016,
                    from:'wxapp'
                }
            },
            classifyData:{
                "self:Goods:Category:getCategoryList":{
                    agencyId:"10000016"
                }
            }
        }
        request.post({
            path: '/wxapp_api/api'
        },datas.indexData)
        .then(function(res){
            var retData = res['self:myshop:homepage:index_data'];
            if(retData.ret != 0){
                // modal.toast(retData.msg);
                wx.navigateTo({
                    url: "../error/error"
                })
                return false;
            }                
            var special = [];
            var dpShop = [];
            var swiper = {};
            var data =retData['data']['data'];
            for (var i in data){
                switch(data[i]['id']){
                    case 2:
                        swiper['data'] = data[i];
                        break;
                    case 3:
                        tempBanner['data'] = data[i];
                        break;
                    case 5:
                        special.push(data[i]);
                        break;
                    case 6:
                        tempCommodity.data = data[i];
                        break;
                    case 7:
                        special.push(data[i]);
                        wx.setStorage({
                            key:'topicId'+data[i]['topicId'],
                            data:data[i]
                        })
                        break;
                }
            }
            tempSpecials = special;
            that.setData({
                comBanner:tempBanner,
                comCommodity:tempCommodity,
                comSpecials:tempSpecials,
                comSwiper:swiper
            });
            modal.hide();
        });

        request.post({
            path: '/wxapp_api/api'
        },datas.classifyData)
        .then(function(res){
            var categoryData = res['self:Goods:Category:getCategoryList'];

            if (categoryData.ret != 0){
                // modal.toast(retData.msg);
                wx.navigateTo({
                    url: "../error/error"
                })
                return false;
            }
            that.setData({
                categoryList:categoryData.data
            });
            
        }); 

        request.post({
            path: '/wxapp_api/api'
        },{'self:cart_api:cart:cartcount':{}})
        .then(function(res){
            var cartData = res['self:cart_api:cart:cartcount'];
            that.setData({
                cartCount : cartData.data
            });
        })

    },
    onShow : function(e){
        var that = this;
        if(app.globalData.cartCountState){
            app.globalData.cartCountState = false;
            request.post({
                path: '/wxapp_api/api'
            },{'self:cart_api:cart:cartcount':{}})
            .then(function(res){
                var cartData = res['self:cart_api:cart:cartcount'];
                that.setData({
                    cartCount : cartData.data
                });
            })
        }
    },
    goCartPage:function() {
        request.bind().then(() => {
            wx.navigateTo({
                url: '../shopping-cart/shopping-cart'
            })
        })
    },
    goCenter:function(){
        wx.redirectTo({
            url: '../center/center'
        })
    },
    wiiTabChange:function(evt){
        var tabId = evt.target.dataset.id;
        this.setData({
            wiiTab:{
                active:tabId
            }
        });
    }
};

utils.assign(pageSetting,shareObj,sliderPanel,banner,itemBanner,commodity);
Page(pageSetting);